//=============================================================================

#include <windows.h>
#include "../../../../inc/aFilesWomb.h"

//=============================================================================

#define BUFFERSIZE 4096

//=============================================================================

aFilesWomb::aFilesWomb()
{
  wchar_t tmpBuffer[BUFFERSIZE] = {0};

  GetCurrentDirectory(BUFFERSIZE, tmpBuffer);

  projectPath = tmpBuffer;
}

//=============================================================================

bool aFilesWomb::afHaveCategory(const aString& name)
{
  aItem<aCategory>* tmpCategory = categories.alGetFirstItem();

  while(tmpCategory)
  {
    if (tmpCategory->data.acGetName() == name)
      return 1;

    tmpCategory = tmpCategory->next;
  }

  return 0;
}

//=============================================================================

afbRet aFilesWomb::afAddCategory(const aString& path, const aString& name)
{
  if (afHaveCategory(name))
    return FWO_CATEGORY_EXIST;

  aString fullPath = projectPath + "\\";
  WIN32_FIND_DATA fnd;

  fullPath += path;
  fullPath += "\\*.*";

  HANDLE found = FindFirstFile(fullPath.asGetString(), &fnd);

  if (found == INVALID_HANDLE_VALUE)
    return FWO_INVALID_PROJECT_PATH;

  categories.alAddItem()->data.acChawFolder(fullPath, name);

  return FWO_DONE;
}

//=============================================================================

aString aFilesWomb::afGetPath(const aString& category, const aString& name)
{
  if ((category.asGetLength() <= 0) || (name.asGetLength() <= 0))
    return "";

  aItem<aCategory>* tmpCategory = categories.alGetFirstItem();

  while(tmpCategory)
  {
    if (tmpCategory->data.acGetName() == category)
      return tmpCategory->data.acGetFullPath(name);

    tmpCategory = tmpCategory->next;
  }

  return "";
}

//=============================================================================

aFilesWomb::~aFilesWomb()
{
}

//=============================================================================
